$(function(){
	
	initEvent();
	
	function initEvent(){
		new PCAS("province","city");
		bankcard_submit();
		initBankCard();
	}
});

/**
 * 初始化 绑定银行信息
 */
function initBankCard(){
	$.ajax({
		url:g_requestContextPath + "/account/initbankcard",
		type:"post",
		data:{},
		success:function(data){
			json = toJson(data); //转json
			//console.log('返回内容：\n'+ JSON.stringify(data));
			if(json.result.status == 1){
				var imgBankHtml = "<img src='/images/account/bankicon/"+json.result.bankCode+".png' alt=''>";
				$('.card_info .img').html(imgBankHtml);
				$('.card_info .code span').html(json.result.bankCardNo);
				$('.card_right .name .right').html(json.result.realName);
				$('.card_right .id_card .right').html(json.result.idCardNo);
				$('.card_right .phone .right').html(json.result.bindMobile);
				//--------------------------
				if(null != json.result.bank){
					new PCAS("province","city",json.result.bank.province,json.result.bank.city);
					set_select_checked('bankType',json.result.bank.bankCode);
					$("#bankCardNo").val(json.result.bank.bankCardNo);
					$("#bindMobile").val(json.result.bank.bindMobile);
					$("#showEditBankCard").css('display','block');
					$("#realName").val(json.result.bank.realName);
					$("#idCardNo").val(json.result.bank.idCardNo);
				}
				
				$(".add_card .card_no").css('display','none');
				$(".bankcard_details .card_info").css('display','block');
			}else{
				$(".add_card .card_no").css('display','block');
				$(".bankcard_details .card_info").css('display','none');
			}
		}
	});
}


/**
 * 实名绑定银行 提交时间 
 */
function bankcard_submit(){
	$('#bankcard_submit').click(function(){
		debugger;
		//var idCardNo = $("#idCardNo").val();
		var city = $("#city option:selected").val();
		var province = $("#province option:selected").val();
		var bankCode = $("#bankType option:selected").val();
		var bankName = $("#bankType option:selected").text();
		var bankCardNo = $("#bankCardNo").val();
		var bindMobile = $("#bindMobile").val();
		
		var realName = $("#realName").val();
		var idCardNo = $("#idCardNo").val();
		
		var boo = isPoneAvailable(bindMobile);
		if(!boo){
			layer.msg("手机格式不正确");
			return;
		}
		var returnCard = isCardID(idCardNo);
		if(returnCard != 'true'){
			layer.msg(returnCard);
			return;
		}
		
		if(!CheckBankNo(bankCardNo)){
			return;
		}
		debugger;
		if('' == $.trim(province)){
			layer.msg("所属省份不能为空");
			return;
		}
		
		if('' == $.trim(city)){
			layer.msg("所属地区不能为空");
			return;
		}
		
		if('' == $.trim(bankCode)){
			layer.msg("所属银行不能为空");
			return;
		}
		
		if('' == $.trim(realName)){
			layer.msg("真实姓名不能为空");
			return;
		}
	//	if('' == $.trim(bankName) || '' == $.trim(idCardNo) || '' == $.trim(city) || '' == $.trim(bankCode) || '' == $.trim(bankCardNo) || '' == $.trim(bindMobile) || ''== $.trim(realName) || '' == $.trim(idCardNo) ){
	//		layer.msg("信息不完整");
	//		return ;
	//	}
		var data = {
				"idCardNo" : idCardNo,
				"city" : city,
				"province" : province,
				"bankCode" : bankCode,
				"bankName" : bankName,
				"bankCardNo" : bankCardNo,
				"bindMobile" : bindMobile,
				"realName" : realName
		}
		$.ajax({
			url:g_requestContextPath + "/account/bankcard",
			type:"post",
			data:data,
			success:function(data){
				json = toJson(data); //转json
				//console.log('返回内容：\n'+ JSON.stringify(data));
				if(!json.result.status){
					layer.msg(json.result.errorMessage);
					return;
				}else{
					layer.msg(json.result.successMessage);
					window.location.href = "/account/bindBankCards";
				}
			}
		});
		
	})
}

